package com.dhcc.sds.standard.controller;


import java.util.List;
import java.util.Map;
import java.util.Objects;


import cn.hutool.core.bean.BeanUtil;
import com.dhcc.cdc.busi.entity.CdcExamdicEntity;
import com.dhcc.cdc.busi.entity.CdcExamitemEntity;
import com.dhcc.cdc.busi.entity.CdcOrdmastEntity;
import com.dhcc.core.framework.security.ShiroKit;
import com.dhcc.core.modules.system.entity.User;
import com.dhcc.sds.standard.dto.CdcExamdicDto;
import com.dhcc.sds.standard.dto.CdcOrdmastDto;
import com.dhcc.sds.standard.service.ISdsStandDicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;



import com.dhcc.sds.standard.entity.SdsStandRisitemMatchEntity;
import com.dhcc.sds.standard.service.ISdsStandRisitemMatchService;


/**
 * 标准化_检查项目配置
 * 
 * @ClassName: SdsStandRisitemMatchController
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-03-22 10:09:53
 */
@Controller
@RequestMapping("${sys.backendPath}/sds/sdsstandrisitemmatch")
public class SdsStandRisitemMatchController extends BaseController {

	private String PREFIX = "/backend/sds/sdsstandrisitemmatch/";

	@Autowired
	private ISdsStandRisitemMatchService sdsStandRisitemMatchService;

    @Autowired
    private ISdsStandDicService sdsStandDicService;
	

    /**
     * 跳转到标准化_检查项目配置列表首页
     */
    @RequestMapping("")
    public String index(Model model) {
        model.addAttribute("dicMap",sdsStandDicService.getDicMapBySource("影像检查"));
        return PREFIX + "sdsstandrisitemmatch.html";
    }
	
    /**
     * 跳转到新增标准化_检查项目配置
     */
    @RequestMapping("/to_add")
    public String toAdd() {
        return PREFIX + "sdsstandrisitemmatch_add.html";
    }
	
	/**
     * 跳转到修改标准化_检查项目配置
     */
    @RequestMapping("/to_update/{sdsStandRisitemMatchId}")
    public String toUpdate(@PathVariable Long sdsStandRisitemMatchId, Model model) {
        SdsStandRisitemMatchEntity sdsStandRisitemMatch = this.sdsStandRisitemMatchService.selectById(sdsStandRisitemMatchId);
        model.addAttribute("sdsStandRisitemMatch",sdsStandRisitemMatch);
        return PREFIX + "sdsstandrisitemmatch_edit.html";
    }

    /**
     * 获取标准化_检查项目配置列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
		Page<SdsStandRisitemMatchEntity> page = new PageFactory<SdsStandRisitemMatchEntity>().defaultPage();
        List<SdsStandRisitemMatchEntity> result = this.sdsStandRisitemMatchService.page(page,params);
        page.setRecords((List<SdsStandRisitemMatchEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增标准化_检查项目配置
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SdsStandRisitemMatchEntity sdsStandRisitemMatch) {
        if (CommonUtil.isOneEmpty(sdsStandRisitemMatch)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        sdsStandRisitemMatch.insert();
        return R.ok();
    }

    /**
     * 删除标准化_检查项目配置
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long sdsStandRisitemMatchId) {
	
        this.sdsStandRisitemMatchService.deleteById(sdsStandRisitemMatchId);

        return R.ok();
    }

    /**
     * 修改标准化_检查项目配置
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(SdsStandRisitemMatchEntity sdsStandRisitemMatch) {
        if (CommonUtil.isOneEmpty(sdsStandRisitemMatch)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsStandRisitemMatchEntity old = this.sdsStandRisitemMatchService.selectById(sdsStandRisitemMatch.getId());

        old.setDicsubId(sdsStandRisitemMatch.getDicsubId());
        old.setExamdicId(sdsStandRisitemMatch.getExamdicId());
        old.setResult(sdsStandRisitemMatch.getResult());
        old.updateAllColumnById();
        return R.ok();
    }

    /**
     * 标准化_检查项目配置对照
     */
    @RequestMapping(value = "/compare")
    @ResponseBody
    public Object compare(@RequestParam Long sdsStandDicsubId, @RequestParam String risitemIds, @RequestParam Long sdsStandDicId, @RequestParam String risorderIds) {
        User user = ShiroKit.getUser();
        String username = user.getLoginName();
        this.sdsStandRisitemMatchService.compare(sdsStandDicsubId, risitemIds, sdsStandDicId, risorderIds, username);

        return R.ok();
    }

    /**
     * 撤销标准化_检查项目配置对照
     */
    @RequestMapping(value = "/undo")
    @ResponseBody
    public Object undo(@RequestParam String risitemIds, @RequestParam Long sdsStandDicsubId, @RequestParam String risorderIds, @RequestParam Long sdsStandDicId) {
        User user = ShiroKit.getUser();
        String username = user.getLoginName();
        this.sdsStandRisitemMatchService.undo(risitemIds, sdsStandDicsubId, risorderIds, sdsStandDicId, username);

        return R.ok();
    }

    /**
     * 自动匹配标准化_检查项目配置
     */
    @RequestMapping(value = "/autoCompareRisitem")
    @ResponseBody
    public Object autoCompareRisitem() {

        this.sdsStandRisitemMatchService.autoCompareRisitem();

        return R.ok();
    }

    /**
     * 自动匹配标准化-检查医嘱
     */
    @RequestMapping(value = "/autoCompareRisorder")
    @ResponseBody
    public Object autoCompareRisorder() {

        this.sdsStandRisitemMatchService.autoCompareRisorder();

        return R.ok();
    }

    /**
     * 获取检查项目字典表数据
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/listExamdic")
    @ResponseBody
    public Object listExamdic(@RequestParam Map<String, Object> params) {

        if (Objects.equals("match", params.get("compare"))){
            Page<SdsStandRisitemMatchEntity> page = new PageFactory<SdsStandRisitemMatchEntity>().defaultPage();
            List<CdcExamdicDto> result = this.sdsStandRisitemMatchService.pageMatchExamdic(page,params);

            Page<CdcExamdicDto> dtoPage = new PageFactory<CdcExamdicDto>().defaultPage();
            BeanUtil.copyProperties(page,dtoPage);
            dtoPage.setRecords(result);
            return R.packForBT(dtoPage);
        }else {
            Page<CdcExamdicEntity> page = new PageFactory<CdcExamdicEntity>().defaultPage();
            List<CdcExamdicDto> result = this.sdsStandRisitemMatchService.pageExamdic(page,params);

            Page<CdcExamdicDto> dtoPage = new PageFactory<CdcExamdicDto>().defaultPage();
            BeanUtil.copyProperties(page,dtoPage);
            dtoPage.setRecords(result);
            return R.packForBT(dtoPage);
        }

    }

    /**
     * 获取检查医嘱项表数据
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/listOrdmast")
    @ResponseBody
    public Object listOrdmast(@RequestParam Map<String, Object> params) {
        Page<CdcOrdmastEntity> page = new PageFactory<CdcOrdmastEntity>().defaultPage();
        List<CdcOrdmastDto> result = this.sdsStandRisitemMatchService.pageOrdmast(page,params);

        Page<CdcOrdmastDto> dtoPage = new PageFactory<CdcOrdmastDto>().defaultPage();
        BeanUtil.copyProperties(page,dtoPage);
        dtoPage.setRecords(result);
        return R.packForBT(dtoPage);
    }

    /**
     * 获取对照检查医嘱项表数据
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/listMatchOrdmast")
    @ResponseBody
    public Object listMatchOrdmast(@RequestParam Map<String, Object> params) {

        if (Objects.equals("match", params.get("compare"))){
            Page<SdsStandRisitemMatchEntity> page = new PageFactory<SdsStandRisitemMatchEntity>().defaultPage();
            List<CdcOrdmastDto> result = this.sdsStandRisitemMatchService.pageMatchOrdmast(page,params);

            Page<CdcOrdmastDto> dtoPage = new PageFactory<CdcOrdmastDto>().defaultPage();
            BeanUtil.copyProperties(page,dtoPage);
            dtoPage.setRecords(result);
            return R.packForBT(dtoPage);
        }else {
            Page<CdcOrdmastEntity> page = new PageFactory<CdcOrdmastEntity>().defaultPage();
            List<CdcOrdmastDto> result = this.sdsStandRisitemMatchService.pageOrdmast(page,params);

            Page<CdcOrdmastDto> dtoPage = new PageFactory<CdcOrdmastDto>().defaultPage();
            BeanUtil.copyProperties(page,dtoPage);
            dtoPage.setRecords(result);
            return R.packForBT(dtoPage);
        }

    }

    /**
     * 对照的标准字典进行渲染
     */
    @RequestMapping(value = "/matchDicsub")
    @ResponseBody
    public Object matchDicsub() {

        List<Long> matchIds = this.sdsStandRisitemMatchService.selectMatchDicsub();

        return matchIds;
    }
	
}
